import appConfig from '@/appConfig'
import store from "@/store";
import {commonApi} from "@/api";

export default {
  getPageShare() {
    return {
      path: store.getters.settings.share_app_path,
      imageUrl: store.getters.settings.share_app_image,
      title: store.getters.settings.share_app_title,
    }
  },


  share(channel) {
    console.log(channel)

    let scene = ''

    if (channel === 'weixin') {
      scene = 'WXSceneSession'
    } else if (channel === 'weixin_circle') {
      scene = 'WXSceneTimeline'
    }

    // #ifdef APP-PLUS
    if (channel === 'weixin_circle') {
      uni.showLoading({
        title: '生成海报中...'
      })

      uni.share({
        provider: "weixin",
        scene,
        type: 2,
        imageUrl: commonApi.getSharePosterImageUrl(store.getters.mine.id),
        success(res) {
          console.log("success111:" + JSON.stringify(res));
        },
        fail(err) {
          console.log("fail:" + JSON.stringify(err));
        },
        complete() {
          uni.hideLoading()
        }
      });
    } else if (channel === 'weixin') {
      uni.showLoading({
        title: '加载中...',
      })
      uni.share({
        provider: 'weixin',
        scene,
        type: 5,
        imageUrl: store.getters.settings.share_app_image,
        title: store.getters.settings.share_app_title,
        miniProgram: {
          id: appConfig.xcxAppId,
          path: 'pages/home/home?inviter=' + store.getters.mine.id,
          type: 0,// 可取值： 0-正式版； 1-测试版； 2-体验版。 默认值为0
          webUrl: 'http://uniapp.dcloud.io'
        },
        success: ret => {
          console.log(JSON.stringify(ret));
        },
        complete() {
          uni.hideLoading()
        }
      })
    }
    // #endif
  },

  async copyShortLink() {
    const res = await commonApi.getInviteWXShortLink()

    uni.setClipboardData({
      data: res.data.data.link,
      success: function () {
        // console.log('success');
        uni.showToast({
          title: '小程序短链复制成功！',
          icon: 'none',
          duration: 2000
        });
      }
    });
  },

  shareScore(channel, imgUrl) {
    console.log(channel + '图片：' + imgUrl)

    let scene = ''

    if (channel === 'weixin') {
      scene = 'WXSceneSession'
    } else if (channel === 'weixin_circle') {
      scene = 'WXSceneTimeline'
    }

    // #ifdef APP-PLUS
    uni.share({
      provider: "weixin",
      scene,
      type: 2,
      imageUrl: imgUrl,
      success: function (res) {
        console.log("success:" + JSON.stringify(res));
      },
      fail: function (err) {
        console.log("fail:" + JSON.stringify(err));
      }
    });
    // #endif


  }
}
